from typing import Optional, List, Dict
from pydantic import BaseModel, Field
from datetime import datetime


class BaseSchema(BaseModel):
    class Config:
        # 允许通过别名访问属性
        allow_population_by_field_name = True
        # 使用驼峰命名的别名
        alias_generator = lambda x: ''.join(word.capitalize() if i else word for i, word in enumerate(x.split('_')))


class UserBase(BaseSchema):
    dept_id: Optional[int] = Field(None, alias="deptId")
    user_name: Optional[str] = Field(None, alias="userName")
    nick_name: Optional[str] = Field(None, alias="nickName")
    user_type: Optional[str] = Field("00", alias="userType")
    email: Optional[str] = ""
    phonenumber: Optional[str] = ""
    sex: Optional[str] = "0"
    avatar: Optional[str] = ""
    status: Optional[str] = "0"
    use_flag: Optional[str] = Field("0", alias="useFlag")
    login_ip: Optional[str] = Field("", alias="loginIp")
    login_date: Optional[datetime] = Field(None, alias="loginDate")
    create_by: Optional[str] = Field("", alias="createBy")
    create_time: Optional[datetime] = Field(None, alias="createTime")
    update_by: Optional[str] = Field("", alias="updateBy")
    update_time: Optional[datetime] = Field(None, alias="updateTime")
    remark: Optional[str] = None


class UserCreate(UserBase):
    password: str
    post_ids: Optional[List[int]] = Field(None, alias="postIds")
    role_ids: Optional[List[int]] = Field(None, alias="roleIds")


class UserUpdate(UserBase):
    password: Optional[str] = None
    post_ids: Optional[List[int]] = Field(None, alias="postIds")
    role_ids: Optional[List[int]] = Field(None, alias="roleIds")


class UserDelete(BaseModel):
    user_ids: List[int] = Field(..., description="用户ID列表")


class PageParams(BaseSchema):
    page_num: int = Field(1, ge=1, alias="pageNum", description="页码")
    page_size: int = Field(10, ge=1, le=100, alias="pageSize", description="每页数量")
    user_name: Optional[str] = Field(None, alias="userName")
    phonenumber: Optional[str] = None
    dept_id: Optional[int] = Field(None, alias="deptId")
    status: Optional[str] = None
    use_flag: Optional[str] = Field(None, alias="useFlag")
    role_id: int = Field(None, alias="roleId")
    is_allocated: int = None
    is_un_allocated: int = None


class UserInfo(UserBase):
    user_id: int = Field(..., alias="userId")
    post_ids: List[int] = Field(default_factory=list, alias="postIds")
    role_ids: List[int] = Field(default_factory=list, alias="roleIds")
    roles: List[Dict] = Field(default_factory=list, alias="roles")

    def dict(self, *args, **kwargs):
        user_dict = super().dict(*args, **kwargs)
        if user_dict.get('createTime'):
            user_dict['createTime'] = user_dict['createTime'].strftime('%Y-%m-%d %H:%M:%S')
        if user_dict.get('updateTime'):
            user_dict['updateTime'] = user_dict['updateTime'].strftime('%Y-%m-%d %H:%M:%S')
        return user_dict


class UserChange(BaseModel):
    user_id: int = Field(..., alias="userId")
    use_flag: Optional[str] = Field("0", alias="useFlag")


class UserProfile(BaseSchema):
    nick_name: Optional[str] = Field(None, alias="nickName")
    email: Optional[str] = ""
    phonenumber: Optional[str] = ""
    sex: Optional[str] = "0"
    create_by: Optional[str] = Field("", alias="createBy")
    create_time: Optional[datetime] = Field(None, alias="createTime")
    update_by: Optional[str] = Field("", alias="updateBy")
    update_time: Optional[datetime] = Field(None, alias="updateTime")
    remark: Optional[str] = None
    password: Optional[str] = None


class UserPassword(BaseModel):
    newPassword: Optional[str] = None
    oldPassword: Optional[str] = None


class UserRole(BaseModel):
    user_id: Optional[int] = Field(None, alias="userId")
    role_ids: Optional[str] = Field(None, alias="roleIds")
